import requests
from DBUtils.PooledDB import PooledDB
import threading
import pandas as pd
import numpy as np
import pymysql
import time
import os
ID = "ww647f8e785f4d83d5"
SECRET = "9lIe12ay42TwCw4glRIDyuzKdWn70jgGjXE0NkcGdYc"
url_get_access_token = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + ID + "&corpsecret=" + SECRET

def pool():
    pool = PooledDB(pymysql, 15, host='localhost', user='root', passwd='zcxvbmn', db='weixindb', port=3306)
    return pool

def clean_client_detail_list():
    db = pool().connection()
    cursor = db.cursor()
    clean_client_count_sql = "delete from client_detail_list_chongqing"
    cursor.execute(clean_client_count_sql)
    db.commit()
    return

#获取access_token的初始值#
access_token_json = requests.get(url_get_access_token).json()
access_token = access_token_json['access_token']

#获取成员列表
url_get_member_list = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + access_token + "&department_id=1&fetch_child=1"
member_list = requests.get(url_get_member_list).json()['userlist']


def get_client_detail_list(userid):
    db = pool().connection()
    cursor = db.cursor()
    i = 1  # i为计数器
    next_cursor = ''
    while i == 1 or next_cursor != '':
        user = {
            "userid": userid,
            "limit": 100,
            "cursor": next_cursor,
        }
        url_get_client_detail_list = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token=" + access_token
        client_detail_list_json = requests.post(url_get_client_detail_list, json=user).json()
        external_contact_list = client_detail_list_json['external_contact_list']
        client_detail_list = []
        insert_sql = ""
        for external_contact in external_contact_list:
            member_userid = (external_contact['follow_info']['userid'],)
            createtime = (repr(external_contact['follow_info']['createtime']),)
            try:
                add_way = (repr(external_contact['follow_info']['add_way']),)
            except:
                add_way = ('',)
            external_userid = (external_contact['external_contact']['external_userid'],)
            type = (repr(external_contact['external_contact']['type']),)
            gender = (repr(external_contact['external_contact']['gender']),)
            tuple_client_detail = member_userid + createtime + add_way + external_userid + type + gender
            insert_sql = "insert into client_detail_list_chongqing(userid,createtime,add_way,external_userid,type,gender) values(%s,%s,%s,%s,%s,%s)"
            client_detail_list.append(tuple_client_detail)
        # 这里要开始尝试用executemany一次插入100条sql#
        cursor.executemany(insert_sql, client_detail_list)
        db.commit()
        try:
            next_cursor = client_detail_list_json['next_cursor']
        except:
            print(client_detail_list_json)
        i = i + 1
        time.sleep(0.02)
    return

def task(start_num, end_num):
    for member in member_list[start_num: end_num]:
        userid = member['userid']
        get_client_detail_list(userid)
    return

def get_client_list_threading_task():
    for n in range(0, len(member_list), 100):
        start_num = n
        end_num = n+99
        task_threading = threading.Thread(target=task, args=(start_num, end_num))
        task_threading.start()
    return

clean_client_detail_list()
get_client_list_threading_task()
